View Javadoc
1 /* 2 QueryJ 3 4 Copyright (C) 2002 Jose San Leandro Armend?riz 5 jsanleandro@yahoo.es 6 chousz@yahoo.com 7 8 This library is free software; you can redistribute it and/or 9 modify it under the terms of the GNU General Public 10 License as published by the Free Software Foundation; either 11 version 2.1 of the License, or (at your option) any later version. 12 13 This library is distributed in the hope that it will be useful, 14 but WITHOUT ANY WARRANTY; without even the implied warranty of 15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 16 Lesser General Public License for more details. 17 18 You should have received a copy of the GNU General Public 19 License along with this library; if not, write to the Free Software 20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 21 22 Thanks to ACM S.L. for distributing this library under the GPL license. 23 Contact info: jsanleandro@yahoo.es 24 Postal Address: c/Playa de Lagoa, 1 25 Urb. Valdecaba?as 26 Boadilla del monte 27 28660 Madrid 28 Spain 29 30 ****************************************************************************** 31 * 32 * Filename: $RCSfile: DataSourceTransactionToken.java,v $ 33 * 34 * Author: Jose San Leandro Armend?riz 35 * 36 * Description: JDBC-related Transaction Token using DataSource class. 37 * 38 * Last modified by: $Author: chous $ at $Date: 2003/08/29 08:15:00 $ 39 * 40 * File version: $Revision: 1.1 $ 41 * 42 * Project version: $Name: $ 43 * 44 * $Id: DataSourceTransactionToken.java,v 1.1 2003/08/29 08:15:00 chous Exp $ 45 * 46 */ 47 package org.acmsl.queryj.dao; 48 49 /* 50 * Importing some ACM-SL classes. 51 */ 52 import org.acmsl.queryj.dao.TransactionToken; 53 54 /* 55 * Importing some JDK1.3 classes. 56 */ 57 import java.sql.Connection; 58 import java.sql.SQLException; 59 import java.io.PrintWriter; 60 61 /* 62 * Importing some extension classes. 63 */ 64 import javax.sql.DataSource; 65 66 /*** 67 * JDBC-related Transaction Token using DataSource class. 68 * @author <a href="mailto:jsanleandro@yahoo.es" 69 >Jose San Leandro Armend?riz</a> 70 * @version $Revision: 1.1 $ 71 */ 72 public abstract class DataSourceTransactionToken 73 implements TransactionToken 74 { 75 /*** 76 * Transaction aliveness. 77 */ 78 private boolean m__bTransactionAlive; 79 80 /*** 81 * Data source released. 82 */ 83 private boolean m__bNotYetReleased; 84 85 /*** 86 * Rollback pending. 87 */ 88 private boolean m__bRollbackPending; 89 90 /*** 91 * DataSource reference. 92 */ 93 private _DataSourceWrapper m__DataSource; 94 95 /*** 96 * Creates a DataSourceTransactionToken using given connection. 97 * @param dataSource the data source to use inside the whole transaction. 98 */ 99 public DataSourceTransactionToken(DataSource dataSource) 100 { 101 setDataSource(dataSource); 102 setNotYetReleased(true); 103 setRollbackPending(false); 104 } 105 106 /*** 107 * Specifies the data source. 108 * @param dataSource the connection to use inside the whole transaction. 109 */ 110 protected void setDataSource(DataSource dataSource) 111 { 112 DataSource t_DataSource = getDataSource(); 113 114 if (t_DataSource != dataSource) 115 { 116 m__DataSource = new _DataSourceWrapper(dataSource); 117 } 118 } 119 120 /*** 121 * Retrieves the data source. 122 * @return the data source used inside the whole transaction. 123 */ 124 public DataSource getDataSource() 125 { 126 return m__DataSource; 127 } 128 129 /*** 130 * Checks if this object is logically equal to given one. 131 * @param object the object to compare to. 132 * @return true if both objects are equal logically. 133 */ 134 public boolean equals(Object object) 135 { 136 boolean result = false; 137 138 DataSource t_DataSource = getDataSource(); 139 140 result = 141 ( (t_DataSource != null) 142 && (t_DataSource.equals(object))); 143 144 if (!result) 145 { 146 result = super.equals(object); 147 } 148 149 return result; 150 } 151 152 /*** 153 * Checks if the transaction is alive. 154 * @return <code>true</code> if so. 155 */ 156 public boolean isTransactionAlive() 157 { 158 return m__bTransactionAlive; 159 } 160 161 /*** 162 * Checks if the transaction is alive. 163 * @return <code>true</code> if so. 164 */ 165 protected void setTransactionAlive(boolean alive) 166 { 167 m__bTransactionAlive = alive; 168 } 169 170 /*** 171 * Checks if the transaction has been released. 172 * @return <code>false</code> if so. 173 */ 174 public boolean isNotYetReleased() 175 { 176 return m__bNotYetReleased; 177 } 178 179 /*** 180 * Specifies if the transaction has been released. 181 * @param releaseState the release state. 182 */ 183 protected void setNotYetReleased(boolean releaseState) 184 { 185 m__bNotYetReleased = releaseState; 186 } 187 188 /*** 189 * Gets notified whenever the transaction starts. 190 */ 191 public void beginTransaction() 192 { 193 setTransactionAlive(true); 194 } 195 196 /*** 197 * Takes into account that the transaction is over. 198 */ 199 public void endTransaction() 200 { 201 setTransactionAlive(false); 202 } 203 204 /*** 205 * Releases the transaction. 206 */ 207 public void release() 208 { 209 if (isNotYetReleased()) 210 { 211 DataSource t_DataSource = getDataSource(); 212 213 if (t_DataSource != null) 214 { 215 // t_DataSource.release(); 216 } 217 218 setNotYetReleased(false); 219 } 220 } 221 222 /*** 223 * Sets the need for a rollback of the whole transaction. 224 * @param flag to indicate to rollback the transaction. 225 */ 226 public void setRollbackPending(boolean flag) 227 { 228 m__bRollbackPending = flag; 229 } 230 231 /*** 232 * Checks the need for a rollback of the whole transaction. 233 * @return such information. 234 */ 235 public boolean isRollbackPending() 236 { 237 return m__bRollbackPending; 238 } 239 240 /*** 241 * Private DataSource wrapper. 242 * @author <a href="mailto:jsanleandro@yahoo.es" 243 >Jose San Leandro Armend?riz</a> 244 * @version $Revision: 1.1 $ 245 */ 246 private static class _DataSourceWrapper 247 implements DataSource 248 { 249 /*** 250 * Concrete wrapped data source. 251 */ 252 private DataSource m__DataSource; 253 254 /*** 255 * The connection used in the transaction. 256 */ 257 private Connection m__Connection; 258 259 /*** 260 * Creates a _DataSourceWrapper using given data source. 261 * @param dataSource the data source to wrap. 262 */ 263 public _DataSourceWrapper(DataSource dataSource) 264 { 265 setDataSource(dataSource); 266 } 267 268 /*** 269 * Specifies the data source. 270 * @param dataSource the data source. 271 */ 272 private void unmodifiableSetDataSource(DataSource dataSource) 273 { 274 m__DataSource = dataSource; 275 } 276 277 /*** 278 * Specifies the data source. 279 * @param dataSource the data source. 280 */ 281 protected void setDataSource(DataSource dataSource) 282 { 283 unmodifiableSetDataSource(dataSource); 284 } 285 286 /*** 287 * Retrieves the data source. 288 * @return the data source. 289 */ 290 public DataSource getDataSource() 291 { 292 return m__DataSource; 293 } 294 295 /*** 296 * Specifies the JDBC connection. 297 * @param connection the connection. 298 */ 299 protected void setConnection(Connection connection) 300 { 301 m__Connection = connection; 302 } 303 304 /*** 305 * Retrieves the JDBC connection. 306 * @return such connection. 307 */ 308 protected Connection getInternalConnection() 309 { 310 return m__Connection; 311 } 312 313 /*** 314 * Attempts to establish a database connection. 315 * @return the connection. 316 */ 317 public Connection getConnection() 318 throws SQLException 319 { 320 Connection result = getInternalConnection(); 321 322 if (result == null) 323 { 324 DataSource t_DataSource = getDataSource(); 325 326 if (t_DataSource != null) 327 { 328 result = t_DataSource.getConnection(); 329 setConnection(result); 330 result.setAutoCommit(false); 331 } 332 } 333 334 return m__Connection; 335 } 336 337 /*** 338 * Attempts to establish a database connection. 339 * @param userName the user name connection settings. 340 * @param password the user password connection settings. 341 * @return the connection. 342 */ 343 public Connection getConnection(String userName, String password) 344 throws SQLException 345 { 346 Connection result = getConnection(); 347 348 if (result == null) 349 { 350 DataSource t_DataSource = getDataSource(); 351 352 if (t_DataSource != null) 353 { 354 result = t_DataSource.getConnection(userName, password); 355 setConnection(result); 356 result.setAutoCommit(false); 357 } 358 } 359 360 return result; 361 } 362 363 /*** 364 * Retrieves the maximum time in seconds that this data source can 365 * wait while attempting to connect to a database. 366 * @return such timeout. 367 */ 368 public int getLoginTimeout() 369 throws SQLException 370 { 371 int result = 0; 372 373 DataSource t_DataSource = getDataSource(); 374 375 if (t_DataSource != null) 376 { 377 result = t_DataSource.getLoginTimeout(); 378 } 379 380 return result; 381 } 382 383 /*** 384 * Retrieves the log writer for this data source. 385 * @return such log writer. 386 */ 387 public PrintWriter getLogWriter() 388 throws SQLException 389 { 390 PrintWriter result = null; 391 392 DataSource t_DataSource = getDataSource(); 393 394 if (t_DataSource != null) 395 { 396 result = t_DataSource.getLogWriter(); 397 } 398 399 return result; 400 } 401 402 /*** 403 * Specifies the maximum time in seconds that this data source will 404 * wait while attempting to connect to a database. 405 * @param seconds the timeout. 406 */ 407 public void setLoginTimeout(int seconds) 408 throws SQLException 409 { 410 DataSource t_DataSource = getDataSource(); 411 412 if (t_DataSource != null) 413 { 414 t_DataSource.setLoginTimeout(seconds); 415 } 416 } 417 418 /*** 419 * Specifies the log writer for this data source. 420 * @param out the log writer. 421 */ 422 public void setLogWriter(PrintWriter out) 423 throws SQLException 424 { 425 DataSource t_DataSource = getDataSource(); 426 427 if (t_DataSource != null) 428 { 429 t_DataSource.setLogWriter(out); 430 } 431 } 432 433 /*** 434 * Releases the transaction. 435 */ 436 public void release() 437 { 438 Connection t_Connection = getInternalConnection(); 439 440 if (t_Connection != null) 441 { 442 try 443 { 444 t_Connection.close(); 445 } 446 catch (SQLException sqlException) 447 { 448 System.out.println("Error releasing Connection."); 449 } 450 451 setConnection(null); 452 } 453 } 454 455 /*** 456 * Checks if the data source is equal logically to given object. 457 * @param object the object to compare to. 458 * @return true if both data sources represents the same entity. 459 */ 460 public boolean equals(Object object) 461 { 462 boolean result = false; 463 464 if ( (object != null) 465 && (object instanceof DataSource)) 466 { 467 DataSource t_InternalDataSource = getDataSource(); 468 469 DataSource t_GivenDataSource = (DataSource) object; 470 471 result = 472 ( (t_GivenDataSource == t_InternalDataSource) 473 || (t_GivenDataSource.equals(t_InternalDataSource))); 474 } 475 476 return result; 477 } 478 } 479 }

This page was automatically generated by Maven